package tr.com.netgis;

import java.util.Date;

import tr.com.netgis.pojo.NGLocation;
import tr.com.netgis.util.Status;
import tr.com.netgis.util.Utility;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.ClipboardManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;

/**
 * 
 * @author NetGis
 *
 */
public class LocationViewActivity extends Activity implements OnClickListener {

	private TextView txtTarihSaat;
	private TextView txtLat, txtLon, txtHiz, txtStatus, lblHiz, lblHarita;
	private ImageView imgStatus;
	private ImageButton btnToMap;
	
	/**
	 * Location which will be viewed.
	 */
	private NGLocation location;

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
	    super.onConfigurationChanged(newConfig);

	    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
			setContentView(R.layout.location_view_landscape);
	    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
			setContentView(R.layout.location_view);
	    }
	    
	    setupActivity();
	    onResume();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.location_view);
		
		setupActivity();
	}
	
	protected void setupActivity (){
		txtTarihSaat = (TextView) findViewById(R.id.locationViewTarihSaat);
		txtLat = (TextView) findViewById(R.id.locationViewTxtLat);
		txtLon = (TextView) findViewById(R.id.locationViewTxtLon);
		txtHiz = (TextView) findViewById(R.id.locationViewTxtSpeed);
		txtStatus = (TextView) findViewById(R.id.locationViewTxtStatus);
		imgStatus = (ImageView) findViewById(R.id.locationViewStatus);
		btnToMap = (ImageButton) findViewById(R.id.locationViewBtnToMap);
		lblHiz = (TextView) findViewById(R.id.locationViewLblHiz);
		//lblHarita = (TextView) findViewById(R.id.locationViewLblHarita);
		
		txtLat.setOnClickListener(this);
		txtLon.setOnClickListener(this);
		
		final Context ctx = this;
		btnToMap.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Toast.makeText(ctx, location.toString(), 4).show();
				GeoPoint p = new GeoPoint((int)(location.getLat() * 1e6), (int)(location.getLon() * 1e6));
				showMap(p);				
			}
		});		
		Bundle bundle = getIntent().getExtras();
		location = (NGLocation) (bundle != null ? bundle.getParcelable("location") : new NGLocation());
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		//onResume operations
		if (location == null) {
			location = new NGLocation();
		}
		txtTarihSaat.setText(Utility.createSimpleDateFormat().format(new Date(location.getTarihSaat())));
		txtLat.setText(Double.toString(location.getLat()));
		txtLon.setText(Double.toString(location.getLon()));
		txtHiz.setText(location.getSpeedView());
		imgStatus.setImageResource(Utility.getImageResource(location));
		txtStatus.setText(Utility.getFriendlyStatus(location.getStatus()));
		
		if (!location.getStatus().equals(Status.HAREKET_EDIYOR))
			lblHiz.setVisibility(View.INVISIBLE);
		else
			lblHiz.setVisibility(View.VISIBLE);
		if (location.getStatus().equals(Status.HAREKET_EDIYOR) || location.getStatus().equals(Status.DURDU)) {
			if (lblHarita != null) lblHarita.setVisibility(View.VISIBLE);
			btnToMap.setVisibility(View.VISIBLE);
		}	
		else {
			if (lblHarita != null) lblHarita.setVisibility(View.INVISIBLE);
			btnToMap.setVisibility(View.INVISIBLE);
		}			
	}
	
	private void showMap(GeoPoint point){
		MapViewActivity.setGeoPoint(point);
		Intent mapActivity = new Intent(this, MapViewActivity.class);
		startActivity(mapActivity);			
	}

	@Override
	public void onClick(View v) {
		ClipboardManager ClipMan = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
		String copied = location.getLon() + ", " + location.getLat();
	    ClipMan.setText(copied);
	    Toast.makeText(getApplicationContext(), "Enlem ve Boylam Kopyalandı\n" + copied, Toast.LENGTH_SHORT).show();
	}
}
